package com.tianxiabuyi.weixin.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.tianxiabuyi.utils.JSONUtil;

/**
 * 微信令牌
 */
public class AccessToken {

    @JSONField(name = "access_token")
    private String accessToken;

    @JSONField(name = "expires_in")
    private long expiresIn;

    private long expiresTill;

    public static AccessToken fromJson(String json) {
        return JSONUtil.fromJson(json, AccessToken.class);
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
        this.expiresTill = System.currentTimeMillis() + (expiresIn * 1000) - 300000;
    }

    public long getExpiresTill() {
        return expiresTill;
    }

    public boolean expired() {
        return System.currentTimeMillis() > expiresTill;
    }
}
